﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TAB45GY
    { 
        public string FROMDATE = "", ENDDATE = "";
        #region ==dwWBH===========
        public static readonly DependencyProperty dwGABProperty = DependencyProperty.Register("dwGAB",
            typeof(DataRow), typeof(TAB45GY),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGABChanged));
        public DataRow dwGAB
        {
            get
            {
                return (DataRow)GetValue(dwGABProperty);
            }
            set
            {
                SetValue(dwGABProperty, value);
            }
        }
        private static void OndwGABChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB45GY;
            if (src != null)
            {
                src.dwGAB = (DataRow)e.NewValue;
            }
        }
        #endregion 
        ITXEXRPT ITXEXRPT;
        public TAB45GY()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABPOS();
            this.DataContext = MV;
            InitializeComponent();
            MapTxt.Add("010", "1799PVA");
            MapTxt.Add("020", "P24");
            MapTxt.Add("030", "JD-136C");
            MapTxt.Add("040", "淀粉");
            MapTxt.Add("050", "丙烯");
            MapTxt.Add("060", "抗静电剂");
            MapTxt.Add("070", "蜡片");
            MapTxt.Add("080", "含固量");
            MapTxt.Add("090", "回潮率");
            MapTxt.Add("100", "配方");
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                QueryCommand(null, null);
            }
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            DataSet Result;
            DataSet Return = new DataSet();
            DataRow dwData = null;
            ART = txtART.Text.ToString().Trim();
            if (FROMDATE == "")
            {
                FROMDATE = "20000000";
            }
            if (ENDDATE == "")
            {
                ENDDATE = "99999999";
            }

            dwData = MV.SetProd.Tables["ABPOS"].NewRow();
            dwData["ABPART"] = ART;
            //dwData["GPBEINDAT"] = FROMDATE;
            // dwData["GPBNEUDAT"] = ENDDATE;
            try
            {
                Result = TMV.QueryABPOS(dwData, 34);
                MergeData(Result, true, true);
                ConvertTXT();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        Dictionary<string, string> MapTxt = new Dictionary<string, string>();
        public void ConvertTXT()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            Dictionary<string, int> txtID = new Dictionary<string, int>();
            string processed = "";
            int pos = 1;
            string txt = "";
            int idx = -1;
            string spliter = "     ";
            string comma = ",";
            string value = "";
            int idx1 = -1;
            int idx2 = -1;
            string txtn = "";
            foreach (DataRow dwTXT in TBMain.Rows)
            {
                txt = Helper.GetGB2312Value(dwTXT["GY"].ToString());
                foreach (KeyValuePair<string, string> kp in MapTxt)
                {
                    string key = kp.Key;//010
                    string content = kp.Value;//JD-136C
                    idx = txt.IndexOf(content);//-1
                    if (idx >= 0)
                    {
                        idx1 = txt.IndexOf(spliter, idx);
                        idx2 = txt.IndexOf(comma, idx);
                        if (idx2 > 0)
                        {
                            if (idx1 < 0)
                            {
                                idx1 = idx2;
                            }
                            else
                            {
                                idx1 = Math.Min(idx1, idx2);
                            }
                        }
                        if (idx1 >= 0)
                        {
                            //3，JD-136C：25，‘      ’，18
                            txtn = txt.Substring(idx, idx1 - idx); //JD-136C：25
                        }
                        else
                        {
                            txtn = txt.Substring(idx);
                        }
                        if (TBMain.Columns.Contains("TXT" + key))
                        {
                            dwTXT["TXT" + key] = txtn.Replace(content, "").Replace(":", "").Replace("：", "").Replace("(", "").Replace(")", "").Replace("KG", "").Replace("%", ""); 
                        }
                    }
                }
            }
        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwGAB = dwMaster;
            if (dwGAB == null)
            {
                TXTISIND = "";
                return;
            }
            TXTISIND = Helper.Left(dwGAB["ABPAUFKRS"].ToString() + dwGAB["ABPAUFLFD"].ToString() + dwGAB["ABPAUFPOS"].ToString().PadRight(60), 60);

        }
        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.dsSource.Merge(MV.SetProd);
                //ITXEXRPT.dsSource.Merge(Se.Env);

                ITXEXRPT.DocType = "JLTAB45GY";

                ITXEXRPT.strExcelFile = @"\JLT\JLTREPORT.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

    }
}
